package cn.sinjet.carassist;

import com.mgroup.wazehelper.R;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import cn.sinjet.model.carsystem.CarModel;
import cn.sinjet.model.mcuupgrade.McuUpgradeModel;
import cn.sinjet.model.mcuupgrade.McuUpgradeModelSimple;
import cn.sinjet.viewmodel.PageDefine;
import cn.sinjet.viewmodel.ViewModel;

public class AboutActivity extends MyActivity implements OnClickListener{
	SinjetApplication app;
	Button btnBack;
	TextView tvAppVersion;
	TextView tvMcuVersion;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);//
		app = SinjetApplication.getInstance();
		//setContentView(R.layout.activity_about);
		LayoutInflater inflater = LayoutInflater.from(this);
		ViewGroup mViewRoot = (ViewGroup)inflater.inflate(R.layout.activity_about,null);
		setContentView(mViewRoot);
//		app.addActivity(this);
		TextView tvVersionCheck = (TextView)mViewRoot.findViewById(R.id.version_check);
		tvVersionCheck.setOnClickListener(this);

		tvMcuVersion = (TextView)mViewRoot.findViewById(R.id.mcu_version);
		tvMcuVersion.setOnClickListener(this);

		tvAppVersion = (TextView)findViewById(R.id.app_version);
		
		btnBack = (Button)mViewRoot.findViewById(R.id.about_back);
		btnBack.setOnClickListener(this);
	}
	
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.version_check:
			Log.d("test", "on app check version");
			CarModel.getInstance().startVersionCheck(true);
			break;
		case R.id.mcu_version:
			if (1 == CarModel.getInstance().devModule.getMCUUpgradeMethod())
			{
				McuUpgradeModel.getInstance().startUpgrade();
			}
			else if (2 == CarModel.getInstance().devModule.getMCUUpgradeMethod())
			{
				McuUpgradeModelSimple.getInstance().startUpgrade();
			}
			break;
		case R.id.about_back:
			finish();
			break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//MainModel.getIns().setContext(this,ViewTag.PAGE_ABOUT);
		ViewModel.getIns().onActivityResume(this, PageDefine.PAGE_ABOUT, this);
		String version = "V1.0";
		try {
			PackageInfo packInfo = getPackageManager().getPackageInfo(
					getPackageName(), 0);
			version = packInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		tvAppVersion.setText(getResources().getString(R.string.app_version)+ version);
//		updateView(R.id.mcu_version);
		
		McuUpgradeModel.getInstance().checkVersion();
	}

//	@Override
//	public void updateView(int tag) {
//		// TODO Auto-generated method stub
//		PropBase prop = ViewModel.getIns().getViewProperty(tag);
//	    if(prop == null)
//	    	return;
//		switch(tag){
//			case R.id.mcu_version:{
//				String str = prop.getStrData();
//				tvMcuVersion.setText(str);
//			}
//			break;
//		}

//	}

}
